package com.x.chalk.service.impl;

import com.x.chalk.dao.UsefulNewsDao;
import com.x.chalk.dto.UsefulNewsDto;
import com.x.chalk.service.intf.UsefulNewsService;
import com.x.chalk.vo.PageVo;
import com.x.chalk.vo.R;
import org.springframework.stereotype.Service;

@Service
public class UsefulNewsServiceImpl implements UsefulNewsService {

    private final UsefulNewsDao dao;

    public UsefulNewsServiceImpl(UsefulNewsDao dao) {
        this.dao = dao;
    }
    /**
     * 查询动态 分页*/
    @Override
    public R queryPage(int page, int size) {
        long total=dao.selectCount();
        long pages=total%size==0?total/size:total/size+1;
        return R.ok(new PageVo<UsefulNewsDto>(total,pages,dao.usefulpage(
                (page-1)*size,size)));
    }

    /**
     * 增加阅读量
     * @param id 阅读的文章id
     * @return
     */
    @Override
    public R updateRead(Integer id) {
        if(dao.updateRead(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R queryDetail(Integer id) {
        return R.ok(dao.selectById(id));
    }
}
